/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package util;

import java.util.HashMap;
import net.sf.appia.core.message.Message;

/**
 *
 * @author Welington
 */
public class Constants {
    private static Debug debug = Debug.getInstance();
    public static final String DEFAULT_MESSAGE_VALUE = "* NO VALUE *";
    public static final String PROCESSES_FILE = "processes.txt";
    public static final int DEFAULT_REQUEST_ID = -1;

    public static final int PROCESS_LABEL = 0;
    public static final int PROCESS_HOST = 1;
    public static final int PROCESS_PORT = 2;
    public static final int PROCESS_IS_CURRENT = 3;
    
    public static CustomProcess SOURCE_PROCESS;

    public static String getLogIdFormat() {
        return "[%02d:%02d:%02d:%03d - %-4s] : %s";
    }
    
    public static String getLogIdAndClassFormat() {
        return "[%02d:%02d:%02d:%03d - %-4s](%-20s) : %s";
    }

    public static Message buildMessage(String content) {
        return new Message(content.getBytes(), 0, content.length());
    }

    public static String extractContent(Message m) {
        if (m != null && m.length() > 0) {
            return new String(m.toByteArray());
        }

        return "";
    }

    public static HashMap<Integer, CustomProcess> readFromFile() {

        HashMap<Integer, CustomProcess> processes = new HashMap<Integer, CustomProcess>();
        
        String[] lines = FileHandler.load(PROCESSES_FILE);

        if (lines == null) {
            debug.println(Debug.ERROR, "File not found");
        } else {
            int pid = 0;
            for (String line : lines) {
                String[] parts = line.split("\\s");

                if (parts.length < 3) {
                    debug.println(Debug.ERROR, "Wrong process description format in line " + (pid + 1) + ". It was expected (ID LABEL HOST PORT [CURRENT]).");
                } else {
                    CustomProcess p = new CustomProcess();
                    p.setId(pid);
                    
                    if (!fillProcess(p, parts))
                        processes.put(p.getId(), p);
                }
                
                pid++;
            }

            if (SOURCE_PROCESS == null)
                debug.println(Debug.ERROR, "Process source was not found in the file");
        }

        return processes;
    }

    private static boolean fillProcess(CustomProcess p, String[] parts) throws NumberFormatException {
        p.setLabel(parts[PROCESS_LABEL]);
        p.setHost(parts[PROCESS_HOST]);
        p.setPort(Integer.parseInt(parts[PROCESS_PORT]));

        if (parts.length > 3 && SOURCE_PROCESS == null) {
            SOURCE_PROCESS = p;
            return true;
        }
        
        return false;
    }
    
    public static Class[] fillClassVector(Class[] provided) {
        Class[] dst = new Class[provided.length];
        int index = 0;
        for (Class c : provided) {
            dst[index] = provided[index];
            index++;
        }
        
        return dst;
    }
}
